import Vue from "vue";
import Router from "vue-router";
import store from "@/store";
import { Message } from "element-ui";
import { getToken } from "@/utils/auth"; // get token from cookie
// 进度条插件
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import Layout from "@/layout";
Vue.use(Router);
export const constantRoutes = [
  {
    name: "Login",
    path: "/login",
    component: () => import("@/views/Login"),
  },

  {
    path: "*",
    component: () => import("@/views/404"),
    hidden: true,
  },

  {
    path: "/",
    component: Layout,
    redirect: "/users/list",
    meta: {
      isShow: false,
    },
    // children: [
    //   {
    //     path: "dashboard",
    //     name: "Dashboard",
    //     component: () => import("@/views/Dashboard/index"),
    //     meta: { title: "首页", icon: "icon-shouye", isShow: true },
    //   },
    // ],
  },
  {
    name: "Users",
    path: "/users",
    component: Layout,
    meta: {
      isShow: true,
    },
    children: [
      {
        name: "UsersList",
        path: "/users/list",
        component: () => import("@/views/Users"),
        meta: { title: "用户管理", icon: "icon-yonghu", isShow: true },
      },
    ],
  },
  {
    name: "ExamInfo",
    path: "/examInfo",
    component: Layout,
    meta: {
      isShow: true,
    },
    children: [
      {
        name: "ExamInfoList",
        path: "/exam/info",
        component: () => import("@/views/ExamInfo"),
        meta: { title: "考场信息", icon: "icon-yonghu", isShow: true },
      },
    ],
  },
  {
    name: "AddCourse",
    path: "/addCourse",
    component: Layout,
    meta: {
      isShow: true,
    },
    // redirect: "/addCourse/list",
    // meta: {
    //   title: "驾校课程",
    //   icon: "icon-dengji",
    // },
    children: [
      {
        name: "AddCourseList",
        path: "/addCourse/list",
        component: () => import("@/views/AddCourse"),
        meta: { title: "课程管理", icon: "icon-rizhi", isShow: true },
      },
      // {
      //   name: "VideoUpload",
      //   path: "/addCourse/video",
      //   component: () => import("@/views/AddCourse/videoUpload.vue"),
      //   meta: { title: "视频上传", icon: "icon-rizhi", isShow: false },
      // },
      // {
      //   name: "VideoList",
      //   path: "/addCourse/videoList",
      //   component: () => import("@/views/Video"),
      //   meta: { title: "视频管理", icon: "icon-yonghu", isShow: false },
      // },
      // {
      //   name: "VideoPlayer",
      //   path: "/addCourse/videoPlayer",
      //   component: () => import("@/views/Video/videoPlay.vue"),
      //   meta: { title: "视频播放", icon: "icon-rizhi", isShow: false },
      // },
    ],
  },
  {
    name: "Video",
    path: "/video",
    component: Layout,
    children: [
      {
        name: "VideoList",
        path: "/video/list",
        component: () => import("@/views/Video"),
        meta: { title: "视频管理", icon: "icon-yonghu", isShow: false },
      },
      {
        name: "VideoPlayer",
        path: "/video/player",
        component: () => import("@/views/Video/videoPlay.vue"),
        meta: { title: "视频播放", icon: "icon-rizhi", isShow: false },
      },
    ],
  },
  {
    name: "DistributionAmount",
    path: "/distributionAmount",
    component: Layout,
    redirect: "/distributionAmount/list",
    meta: {
      title: "分销金额",
      icon: "icon-dengji",
      isShow: true,
    },
    children: [
      {
        name: "AmountDetail",
        path: "/distributionAmount/list",
        component: () => import("@/views/DistributionAmount"),
        meta: { title: "分销明细", icon: "icon-rizhi", isShow: true },
      },
      {
        name: "AmountInquire",
        path: "/addCourse/inquire",
        component: () => import("@/views/DistributionAmount/AmountInquire.vue"),
        meta: { title: "金额查询", icon: "icon-rizhi", isShow: true },
      },
    ],
  },
  // {
  //   name: "ArrangeGrade",
  //   path: "/grade",
  //   component: Layout,
  //   children: [
  //     {
  //       name: "GradeList",
  //       path: "/grade/list",
  //       component: () => import("@/views/ArrangGrade"),
  //       meta: { title: "排班管理", icon: "icon-rizhi" },
  //     },
  //   ],
  // },
  // {
  //   name: "Stock",
  //   path: "/stock",
  //   component: Layout,
  //   redirect: "/stock/goods/list",
  //   meta: {
  //     title: "库存管理",
  //     icon: "icon-dengji",
  //   },
  //   children: [
  //     {
  //       name: "Goods",
  //       path: "goods/list",
  //       component: () => import("@/views/Stock/Goods"),
  //       meta: {
  //         title: "物品管理",
  //       },
  //     },
  //     {
  //       name: "Supplier",
  //       path: "supplier/list",
  //       component: () => import("@/views/Stock/Supplier"),
  //       meta: {
  //         title: "供应商管理",
  //       },
  //     },
  //     {
  //       name: "Detailed",
  //       path: "detailed/list",
  //       component: () => import("@/views/Stock/Detailed"),
  //       // redirect: '/stock/detailed/equipment/list',
  //       meta: {
  //         title: "库存明细",
  //       },
  //     },
  //     {
  //       name: "EnterStore",
  //       path: "enterStore/list",
  //       component: () => import("@/views/Stock/EnterStore"),
  //       meta: {
  //         title: "入库管理",
  //       },
  //     },
  //     {
  //       name: "OutStore",
  //       path: "outStore/list",
  //       component: () => import("@/views/Stock/OutStore"),
  //       meta: {
  //         title: "出库管理",
  //       },
  //     },
  //   ],
  // },
  // {
  //   name: "Berth",
  //   path: "/berth",
  //   component: Layout,
  //   children: [
  //     {
  //       name: "BerthList",
  //       path: "/berth/list",
  //       component: () => import("@/views/Berth"),
  //       meta: { title: "床位管理", icon: "icon-a-iconchuangwei" },
  //     },
  //   ],
  // },
  // {
  //   name: "TheOld",
  //   path: "/theOld",
  //   component: Layout,
  //   children: [
  //     {
  //       name: "TheOldList",
  //       path: "/theOld/list",
  //       component: () => import("@/views/TheOld"),
  //       meta: { title: "老人管理", icon: "icon-laoren-shi" },
  //     },
  //   ],
  // },
  // {
  //   name: "Body",
  //   path: "/body",
  //   component: Layout,
  //   children: [
  //     {
  //       name: "BodyList",
  //       path: "/body/list",
  //       component: () => import("@/views/Body"),
  //       meta: { title: "身体指标", icon: "icon-jiankang" },
  //     },
  //   ],
  // },
  // {
  //   name: "Pharmacy",
  //   path: "/pharmacy",
  //   component: Layout,
  //   children: [
  //     {
  //       name: "PharmacyList",
  //       path: "/pharmacy/list",
  //       component: () => import("@/views/Pharmacy"),
  //       meta: { title: "用药服务", icon: "icon-yongyaotixing" },
  //     },
  //   ],
  // },
  // {
  //   name: "Modify",
  //   path: "/modify",
  //   component: Layout,
  //   children: [
  //     {
  //       name: "ModifyList",
  //       path: "/modify/list",
  //       component: () => import("@/views/Modify"),
  //       meta: { title: "修改密码", icon: "icon-xiugaimima" },
  //     },
  //   ],
  // },
];
export const asyncRoutes = [];
//常量路由:就是不关用户是什么角色，都可以看见的路由
//什么角色（超级管理员，普通员工）：登录、404、首页
// export const constantRoutes = [
//   {
//     name: 'Login',
//     path: '/login',
//     component: () => import("@/views/Login")
//   },

//   {
//     path: '/404',
//     component: () => import('@/views/404'),
//     hidden: true
//   },

//   {
//     path: '/',
//     component: Layout,
//     redirect: '/dashboard',
//     children: [{
//       path: 'dashboard',
//       name: 'Dashboard',
//       component: () => import('@/views/Dashboard/index'),
//       meta: { title: '首页', icon: 'icon-shouye' }
//     }]
//   },
// ]
// export const asyncRoutes = [
//   {
//     name: 'Users',
//     path: '/users',
//     component: Layout,
//     children: [{
//       name: "UsersList",
//       path: '/users/list',
//       component: () => import("@/views/Users"),
//       meta: { title: '用户管理', icon: 'icon-yonghu' }
//     }]
//   },
//   {
//     name: 'ArrangeGrade',
//     path: '/grade',
//     component: Layout,
//     children: [{
//       name: "GradeList",
//       path: '/grade/list',
//       component: () => import("@/views/ArrangGrade"),
//       meta: { title: '排班管理', icon: 'icon-rizhi' }
//     }]
//   },
//   {
//     name: 'Stock',
//     path: '/stock',
//     component: Layout,
//     redirect: '/stock/goods/list',
//     meta: {
//       title: '库存管理', icon: 'icon-dengji'
//     },
//     children: [
//       {
//         name: 'Goods',
//         path: 'goods/list',
//         component: () => import('@/views/Stock/Goods'),
//         meta: {
//           title: '物品管理',
//         },
//       },
//       {
//         name: 'Supplier',
//         path: 'supplier/list',
//         component: () => import('@/views/Stock/Supplier'),
//         meta: {
//           title: '供应商管理',
//         },
//       },
//       {
//         name: 'Detailed',
//         path: 'detailed/list',
//         component: () => import('@/views/Stock/Detailed'),
//         // redirect: '/stock/detailed/equipment/list',
//         meta: {
//           title: '库存明细',
//         },
//       },
//       {
//         name: 'EnterStore',
//         path: 'enterStore/list',
//         component: () => import('@/views/Stock/EnterStore'),
//         meta: {
//           title: '入库管理',
//         },
//       },
//       {
//         name: 'OutStore',
//         path: 'outStore/list',
//         component: () => import('@/views/Stock/OutStore'),
//         meta: {
//           title: '出库管理',
//         },
//       }
//     ]
//   },
//   {
//     name: 'Berth',
//     path: '/berth',
//     component: Layout,
//     children: [{
//       name: "BerthList",
//       path: '/berth/list',
//       component: () => import("@/views/Berth"),
//       meta: { title: '床位管理', icon: 'icon-a-iconchuangwei' }
//     }]
//   },
//   {
//     name: 'TheOld',
//     path: '/theOld',
//     component: Layout,
//     children: [{
//       name: "TheOldList",
//       path: '/theOld/list',
//       component: () => import("@/views/TheOld"),
//       meta: { title: '老人管理', icon: 'icon-laoren-shi' }
//     }]
//   },
//   {
//     name: 'Body',
//     path: '/body',
//     component: Layout,
//     children: [{
//       name: "BodyList",
//       path: '/body/list',
//       component: () => import("@/views/Body"),
//       meta: { title: '身体指标', icon: 'icon-jiankang' }
//     }]
//   },
//   {
//     name: 'Pharmacy',
//     path: '/pharmacy',
//     component: Layout,
//     children: [{
//       name: "PharmacyList",
//       path: '/pharmacy/list',
//       component: () => import("@/views/Pharmacy"),
//       meta: { title: '用药服务', icon: 'icon-yongyaotixing' }
//     }]
//   },
//   {
//     name: 'Modify',
//     path: '/modify',
//     component: Layout,
//     children: [{
//       name: "ModifyList",
//       path: '/modify/list',
//       component: () => import("@/views/Modify"),
//       meta: { title: '修改密码', icon: 'icon-xiugaimima' }
//     }]
//   }
//   // {
//   //   path: '/404',
//   //   component: () => import('@/views/404'),
//   //   hidden: true
//   // },
//   // {
//   //   path: "*",
//   //   redirect: "/404"
//   // }
// ]

//任意路由：当路径出现错误的时候重定向404
export const anyRoutes = { path: "*", redirect: "/404", hidden: true };

const createRouter = () =>
  new Router({
    mode: "history", // require service support
    //返回的这个y=0，代表的滚动条在最上方
    scrollBehavior: () => ({ y: 0 }),
    base: process.env.BASE_URL,
    //因为注册的路由是‘死的’，‘活的’路由如果根据不同用户（角色）可以展示不同菜单
    routes: constantRoutes,
  });
// const router = new VueRouter({
//   mode: 'history',
//   base: process.env.BASE_URL,
//   routes,
// })
const router = createRouter();

router.beforeEach(async (to, from, next) => {
  NProgress.start();
  let token = getToken("token");
  if (token) {
    if (to.path === "/login") {
      next({ path: "/" });
      NProgress.done();
    } else {
      next();
      // const hasGetUserInfo = store.getters.name;
      // console.log(hasGetUserInfo, "ymy");
      // if (hasGetUserInfo) {
      //   // await store.dispatch('getInfo')
      //   next();
      // } else {
      //   try {
      //     // get user info
      //     await store.dispatch("getInfo");
      //     next({ ...to });
      //   } catch (error) {
      //     await store.dispatch("resetToken");
      //     Message.error(error || "Has Error");
      //     // next(`/login?redirect=${to.path}`)
      //     next(`/login?redirect=${to.path}`);
      //     NProgress.done();
      //   }
      // }
    }
  } else {
    if (to.path == "/login") {
      next();
    } else {
      // next(`/login?redirect=${to.path}`)
      next(`/login`);
      NProgress.done();
    }
  }
});
router.afterEach(() => {
  NProgress.done();
});
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
